package xu.ye.view;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import xu.ye.bean.GroupBean;
import xu.ye.view.adapter.GroupContactAdapter;

import com.agent.crm.bean.MyContact;
import com.agent.crm.db.DBInitScript;
import com.agent.crm.db.DatabaseManager;
import com.agent.crm.util.DateUtils;
import com.agent.crm.view.R;
import com.pwp.activity.ScheduleView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class GroupContactActivity extends Activity{
    
    private ListView groupList;
    private GroupContactAdapter groupAdapter;
    private DatabaseManager manager = null;
    
    private Button addGroupButton;
    //新增请求码
    private static int ADD_REQUEST_CODE = 1;
    //编辑请求码
    private static int EDIT_REQUEST_CODE = 2;
    //添加人员请求码
    private static int ADD_CONTACT_REQUEST_CODE = 3;
    //群发短信请求码
    private static int GROUP_SMS_REQUEST_CODE = 4;
    //群组任务请求码
    private static final int TASK_ADD_REQUEST_CODE = 5;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group_contact);
        groupList = (ListView)findViewById(R.id.groupContactListId);
        addGroupButton = (Button)findViewById(R.id.addGroupButtonId);
        addGroupButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(GroupContactActivity.this, AddEditGroupActivity.class);
                startActivityForResult(intent, ADD_REQUEST_CODE);
            }
        });
        manager = new DatabaseManager(this);
        ((Button)findViewById(R.id.back)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        
        init();
    }
    
    //group组
    private String[] groupCloumns = new String[]{"group_id", "group_name", "group_count"};
    
    private void init(){
        SQLiteDatabase SQLite = manager.getWritableDatabase();
        Cursor cursor = SQLite.query(DBInitScript.Table_GroupInfo, groupCloumns, null, null, null, null, "group_id asc");
        int count = cursor.getCount();
        if(count <=0){
            return ;
        }
        List<GroupBean> groupBeanList = new ArrayList<GroupBean>();
        cursor.moveToFirst();
        for(int i = 0; i < count; i++){
            GroupBean group = new GroupBean();
            cursor.moveToPosition(i);
            int groupId = cursor.getInt(0);
            String groupName = cursor.getString(1);
            int groupCount = cursor.getInt(2);
            group.setId(groupId);
            group.setName(groupName);
            group.setCount(groupCount);
            groupBeanList.add(group);
        }
        cursor.close();
        SQLite.close();
        groupAdapter = new GroupContactAdapter(getApplicationContext(), groupBeanList);
        groupList.setAdapter(groupAdapter);
        
        groupList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GroupBean group = (GroupBean)groupAdapter.getItem(position);
                showGroupDialog(groupDialogList, group, position);
            }
        });
    }
    private String[] groupDialogList = new String[] {"修改组名", "组员编辑", "群发消息", "添加任务"};
    
    private void showGroupDialog(String[] groupDialogList, final GroupBean group, int position){
        new AlertDialog.Builder(GroupContactActivity.this).setTitle(group.getName()).setItems(groupDialogList, new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                
                switch(which){
                    case 0://修改组名
                        Intent intent = new Intent();
                        intent.putExtra("group_id", group.getId());
                        intent.putExtra("group_name", group.getName());
                        intent.putExtra("edit", true);
                        intent.setClass(GroupContactActivity.this, AddEditGroupActivity.class);
                        startActivityForResult(intent, EDIT_REQUEST_CODE);
                        break;
                        
                    case 1://组员编辑
                        Intent contactSelIntent = new Intent();
                        contactSelIntent.putExtra("group_id", group.getId());
                        //1表示分类 2表示群组
                        contactSelIntent.putExtra("work_type", ContactSelectActivity.WORK_TYPE_GROUP);
                        contactSelIntent.setClass(GroupContactActivity.this, ContactSelectActivity.class);
                        startActivityForResult(contactSelIntent, ADD_CONTACT_REQUEST_CODE);
                        break;
                        
                    case 2://群发消息
                        Intent groupSmsIntent = new Intent();
                        groupSmsIntent.putExtra("group_id", group.getId());
                        groupSmsIntent.setClass(GroupContactActivity.this, GroupSmsActivity.class);
                        startActivityForResult(groupSmsIntent, GROUP_SMS_REQUEST_CODE);
                        break;
                    case 3://添加任务
                        Intent intentAdd = new Intent();
                        Calendar c = Calendar.getInstance();
                        int year = c.get(Calendar.YEAR);
                        int month = c.get(Calendar.MONTH) + 1;
                        int day = c.get(Calendar.DAY_OF_MONTH);
                        String ch_dayOfWeek = DateUtils.getWeek(c.getTime());
                        ArrayList<String> scheduleDate = new ArrayList<String>();
                        scheduleDate.add(year + "");
                        scheduleDate.add(month + "");
                        scheduleDate.add(day + "");
                        scheduleDate.add(ch_dayOfWeek);
                        intentAdd.putStringArrayListExtra("scheduleDate", scheduleDate);
                            intentAdd.putExtra("objectID", String.valueOf(group.getId()));
                            intentAdd.putExtra("objectName", group.getName());
                            intentAdd.putExtra("scheduleMode", "2");
                        intentAdd.putExtra("operateType", "add");
                        intentAdd.putExtra("from", "GroupContactActivity");
                        intentAdd.setClass(GroupContactActivity.this, ScheduleView.class);
                        startActivityForResult(intentAdd, TASK_ADD_REQUEST_CODE);
                        break;
                }
                
                
                
            }
            
        }).show();
    }
    
    
    
    
    
    
    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == ADD_REQUEST_CODE){
            init();
        }
        else if(requestCode == EDIT_REQUEST_CODE){
            init();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    
    
    
}
